/**
 * http://www.lnzb.cn/lnzbtb/
 * 辽宁工程建设信息网爬虫
 * sunny 2017-06-14
 */

import { Base, Item } from "../base";

// 工具类
const utils = require('../../utils');

//辽宁工程建设信息网
const lnzbtb_url = "http://www.lnzb.cn/lnzbtb/ShowInfo/zbjgmore.aspx?categorynum=003003005&QuYu=";

class lnzbtb_spider extends Base {
    form: any;
    firstPage: string;
    cacheContent: string;

    constructor() {
        super();
        this.name = "辽宁工程建设信息网_其他";
        this.enabled = true;
        this.encode = "utf-8";
        this.collection = "lnjsgcxxw_qt";
        this.debug = false;
        this.timeout = 1000000;
        this.keyField = "url";
    }

    getPageCount(): number {
        this.form = {};
        var content = this.spider.getSync(lnzbtb_url);
        this.firstPage = content;
        if (/总页数：.*?\<b\>(\d+)/.test(content)) {
            var pgCount = parseInt(RegExp.$1);
            console.log("Page Count: " + pgCount);
            return pgCount;
        }
        return 1;
    }

    getItems(pageIndex: number): Item[] {
        var content: string;
        if (pageIndex == 0) {
            content = this.firstPage;
            this.cacheContent = content;
            var $ = this.cheerio.load(content);
            return this.extractItems($, pageIndex, content);
        } else {
            /**
             * __EVENTTARGET:
             * __EVENTARGUMENT:3
             * 
             */
            var $ = this.cheerio.load(this.cacheContent);
            this.form['__VIEWSTATE'] = $("#__VIEWSTATE").val();
            this.form["__EVENTARGUMENT"] = pageIndex;
            this.form["__EVENTTARGET"] = 'MoreInfoList1$Pager';
            content = this.spider.postSync(lnzbtb_url, this.form);
            this.cacheContent = content;
            var $ = this.cheerio.load(content);
            return this.extractItems($, pageIndex, content);
        }
    }

    private extractItems($: CheerioSelector, pageIndex: number, data: string): Item[] { // 获取页面中的招标项目信息，$是一个类似jQuery的对象(cheerio)，可以直接按照jQuery方式使用
        var items: Item[] = [];
        var urls: string[] = [];
        $("#zbjgmore2_DataGrid1").find("td a").each(function (index, aitem: any) { urls.push($(aitem).attr("href")); });
        urls.forEach(url => {
            items.push(this._extractItemInfo(url));
        });
        return items;
    }

    private _extractItemInfo(url: string): Item {
        let item = this.buildItem();
        url = 'http://www.lnzb.cn' + url;
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        /*
VM418:1 0 "标段编号"
VM418:1 1 "开标日期"
VM418:1 2 "工程名称"
VM418:1 3 "建设单位"
VM418:1 4 "工程类别"
VM418:1 5 "招标方式"
VM418:1 6 "建设地点"
VM418:1 7 "中标范围和内容"
VM418:1 8 "代理机构"
VM418:1 9 "中标单位"
VM418:1 10 "中标价"
VM418:1 11 "建筑面积"
VM418:1 12 "中标单价（元/万平米）"
VM418:1 13 "项目负责人姓名"
VM418:1 14 "项目负责人资格	"
VM418:1 15 "项目负责人IC卡号"
VM418:1 16 "有效工期（天）"

        */
        let fields = ["标段编号", "开标日期", "工程名称", "建设单位", "工程类别", "招标方式", "建设地点", "中标范围和内容", "代理机构", "中标单位", "中标价", "建筑面积", "中标单价（元/万平米）", "项目负责人姓名", "项目负责人资格", "项目负责人IC卡号", "有效工期（天）"];
        $("td.B").each((idx, itm) => {
            (item as any)[fields[idx]] = $(itm).text().trim();
            // console.log($(itm).text().trim());
        });
        item.title = (item as any)['工程名称'];
        (item as any).url = url;
        return item;
    }
}

export = new lnzbtb_spider();
